package dev.entao.travel.admin

import dev.entao.base.Label
import dev.entao.base.Now
import dev.entao.core.Action
import dev.entao.core.HttpContext
import dev.entao.core.render.sendBack
import dev.entao.core.render.sendResult
import dev.entao.html.div
import dev.entao.html.linkButton
import dev.entao.page.*
import dev.entao.page.bs.tableActions
import dev.entao.sql.AND
import dev.entao.sql.ConstraintException
import dev.entao.sql.EQ
import dev.entao.sql.update
import dev.entao.travel.AdminPages
import dev.entao.travel.model.Merchant

@Label("商家管理")
@NavGroup(icon = "static/admin.png")
class MerchantPages(context: HttpContext) : AdminPages(context) {

    @SortParams
    @PageParams
    @NavItem(order = 0, icon = "static/admin.png")
    @Label("查询")
    @Action
    fun list() {
        val thisAction = ::list
        val w = EQS(Merchant::id, Merchant::phone, Merchant::status) AND Merchant::name.LIKE_IN() AND Merchant::adminName.LIKE_IN()
        val rowCount = Merchant.count(w)
        val rowList = Merchant.list(w) {
            orderPage(thisAction)
            limitPage(thisAction)
        }

        dashboard {
            workTitle("商家查询")
            workToolbar {
                linkButton("btn", "btn-sm", "btn-outline-primary") {
                    fromAction(::create)
                }
            }
            workspace {
                div("col-xxl-10", "col-xl-11", "col-lg-12") {
                    queryForm(::list) {
                        queryCol4 {
                            labelEdit(Merchant::id)
                        }
                        queryCol4 {
                            labelEdit(Merchant::name)
                        }
                        queryCol4 {
                            labelSelect(Merchant::status)
                        }
                        queryCol4 {
                            labelEdit(Merchant::phone)
                        }
                        queryCol4 {
                            labelEdit(Merchant::adminName)
                        }

                    }

                    tableActions(::enable, ::disable, this@MerchantPages::delete)
                    tableX(rowList) {
                        itemIDProp(Merchant::id)
                        columnCheck()
                        columnProp(Merchant::id).sortable()
                        columnProp(Merchant::name).sortable()
                        columnProp(Merchant::status).sortable()
                        columnProp(Merchant::phone).sortable()
                        columnProp(Merchant::adminName).sortable()
                        columnProp(Merchant::servicePhone).sortable()
                        sortAction(::list)
                        columnAction(::view, ::edit, ::del)
                    }
                    tablePaginationX(::list, rowCount)
                }
            }
        }
    }

    @ConfirmMessage("要删除这些记录吗?")
    @Label("删除")
    @Action
    fun delete(keys: String) {
        val ns = keys.split(',').map { it.toInt() }
        for (n in ns) {
            Merchant.deleteByKey(n)
        }
        context.sendBack {
            removeKey("keys")
            setSuccess("操作成功")
        }
    }

    @Label("启用")
    @Action
    fun enable(keys: String) {
        val ns = keys.split(',').map { it.toInt() }
        for (n in ns) {
            Merchant.update(Merchant::status to Merchant.ST_ENABLE, Merchant::id EQ n)
        }
        context.sendBack {
            removeKey("keys")
            setSuccess("操作成功")
        }
    }

    @Label("禁用")
    @Action
    fun disable(keys: String) {
        val ns = keys.split(',').map { it.toInt() }
        for (n in ns) {
            Merchant.update(Merchant::status to Merchant.ST_DISABLE, Merchant::id EQ n)
        }
        context.sendBack {
            removeKey("keys")
            setSuccess("操作成功")
        }
    }

    @ConfirmMessage("要删除这条记录吗?")
    @Label("删除")
    @Action
    fun del(id: Int) {
        Merchant.deleteByKey(id)
        context.sendBack {
            removeKey("id")
            setSuccess("已删除:ID=$id 的记录")
        }
    }

    @DialogPop
    @Label("查看")
    @Action
    fun view(id: Int) {
        val row = Merchant.oneByKey(id) ?: return
        sendDialog {
            largeSize800()
            dialogTitle { +"查看" }
            dialogContainer {
                divRow("my-2") {
                    divCol3 {
                        viewProp(row::id)
                    }
                    divCol3 {
                        viewProp(row::name)
                    }
                    divCol3 {
                        viewProp(row::status)
                    }
                    divCol3 {
                        viewProp(row::hasBed)
                    }
                    divCol3 {
                        viewProp(row::adminName)
                    }
                    divCol3 {
                        viewProp(row::phone)
                    }
                    divCol3 {
                        viewProp(row::identNum)
                    }
                    divCol3 {
                        viewProp(row::province)
                    }
                    divCol3 {
                        viewProp(row::city)
                    }
                    divCol12 {
                        viewProp(row::address)
                    }
                    divCol3 {
                        viewProp(row::locationType)
                    }
                    divCol3 {
                        viewProp(row::longitude)
                    }
                    divCol3 {
                        viewProp(row::latitude)
                    }
                    divCol3 {
                        viewProp(row::company)
                    }
                    divCol3 {
                        viewProp(row::zhizhao)
                    }
                    divCol3 {
                        viewProp(row::servicePhone)
                    }
                    divCol3 {
                        viewProp(row::serviceWX)
                    }
                    divCol3 {
                        viewProp(row::serviceQQ)
                    }
                    divCol12 {
                        viewProp(row::notes)
                    }
                }
            }

        }
    }

    @DialogPop
    @Label("编辑")
    @Action
    fun edit(id: Int) {
        val row = Merchant.oneByKey(id) ?: return
        sendDialog {
            dialogTitle { +"编辑" }
            largeSize800()
            dialogForm(this@MerchantPages::update) {
                divRow("my-2") {
                    divCol6 {
                        labelEdit(row::id) {
                            readonly()
                        }
                    }
                    divCol6 {
                        labelEdit(row::name)
                    }
                }
                divRow("my-2") {

                    divCol6 {
                        labelEdit(row::phone)
                    }
                    divCol6 {
                        labelEdit(row::pwd)
                    }
                }
                divRow("my-2") {
                    divCol6 {
                        labelEdit(row::adminName)
                    }
                    divCol6 {
                        labelEdit(row::identNum)
                    }
                }
                divRow("my-2") {

                    divCol6 {
                        labelSelect(row::status)
                    }
                    divCol6 {
                        labelSelect(row::hasBed)
                    }
                }
                divRow("my-2") {
                    divCol6 {
                        labelEdit(row::company)
                    }
                    divCol6 {
                        labelEdit(row::zhizhao)
                    }

                }
                divRow("my-2") {
                    div("col") {
                        labelEdit(row::address)
                    }
                }
                divRow("my-2") {
                    divCol6 {
                        labelEdit(row::province)
                    }
                    divCol6 {
                        labelEdit(row::city)
                    }
                }
                divRow("my-2") {
                    divCol4 {
                        labelSelect(row::locationType)
                    }
                    divCol4 {
                        labelEdit(row::longitude)
                    }
                    divCol4 {
                        labelEdit(row::latitude)
                    }
                }

                divRow("my-2") {
                    divCol4 {
                        labelEdit(row::servicePhone)
                    }
                    divCol4 {
                        labelEdit(row::serviceWX)
                    }
                    divCol4 {
                        labelEdit(row::serviceQQ)
                    }
                }
                divRow("my-2") {
                    div("col") {
                        labelTextArea(row::notes)
                    }
                }
            }
        }
    }

    @Action
    fun update(id: Int) {
        try {
            val a = Merchant.oneByKey(id) ?: return
            a.updateTime = timeNow
            a.update {
                it.fromRequest()
            }
            context.sendResult {
                success("已保存")
            }
        } catch (ex: ConstraintException) {
            context.sendResult {
                failed("手机冲突")
            }
        }
    }

    @DialogPop
    @Label("添加")
    @Action
    fun create() {
        sendDialog {
            dialogTitle { +"添加商家" }
            dialogForm(::save) {
                div {
                    labelEdit(Merchant::name)
                }
                div {
                    labelEdit(Merchant::zhizhao)
                }
                div {
                    labelEdit(Merchant::adminName)
                }
                div {
                    labelEdit(Merchant::identNum)
                }
                div {
                    labelEdit(Merchant::phone)
                }
                div {
                    labelEdit(Merchant::pwd)
                }

                div {
                    labelSelect(Merchant::status)
                }
            }
        }
    }

    @Action
    fun save() {
        try {
            val a = Merchant()
            a.createDate = Now.dateSQL
            a.createTime = Now.time
            a.updateTime = Now.millis
            a.fromRequest()
            a.insert()
            sendResult {
                success()
            }
        } catch (ex: ConstraintException) {
            sendResult { failed("手机号已存在") }
        }
    }
}